package in.muton.edushare.summary.html;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.processing.objects.DeptDataHandler;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.CSSPackageResource;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.GridView;
import org.apache.wicket.model.IModel;

import com.edushare.common.dao.DepartmentsDAO;
import com.edushare.common.vo.Batches;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Curricula;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Users;

public class DeptSummary extends WebPage {
	public DeptSummary(IModel<Departments> dept) {
		init(dept);
	}

	public DeptSummary(PageParameters params) {
		int id = params.getAsInteger("0", 0);
		Departments d = new DepartmentsDAO(EduShareSession.get()
				.getSessionORMInterface()).getDepartmentByPk(id);
		init(new CayenneModel<Departments>(d));
	}

	private void init(IModel<Departments> dept) {
		this.deptModel = dept;
		Departments d = dept.getObject();
		ddh = new DeptDataHandler(deptModel);
		String owner = ddh.getOwner().getObject().getUniversityName();
		add(CSSPackageResource.getHeaderContribution(DeptSummary.class, "PrinterFriendlyCSS.css"));
		add(new Label("title", d.getDeptName()));
		add(new Label("deptName", d.getDeptName()));
		add(new Label("univName", owner));

		CayenneList<Curricula> currList = new CayenneList<Curricula>(d
				.getCurriculums());
		add(new Label("progCount", ""+currList.getObject().size()));
		
		add(new ListView<IModel<Curricula>>("programsLV", currList.getObject()) {
			private static final long serialVersionUID = 6153133506285940405L;

			@Override
			protected void populateItem(ListItem<IModel<Curricula>> arg0) {
				final Curricula c = arg0.getModelObject().getObject();
				arg0.add(new Label("progName", c.getCurriculaName()));
				arg0
						.add(new Label("semLength",c.getTotalSemester()
								.toString() +" semesters"));

				CayenneList<Batches> bl = new CayenneList<Batches>(c
						.getBatches());
				arg0.add(new ListView<IModel<Batches>>("batchLV", bl) {
					private static final long serialVersionUID = -2530163102360252860L;

					@Override
					protected void populateItem(ListItem<IModel<Batches>> arg0) {
						Batches b = arg0.getModelObject().getObject();
						arg0.add(new Label("batchName", b.getStartingSemester()
								.getSemesterName()
								+ ", " + b.getBatchYear()));
					}
				});
			}
		});

		CayenneList<Courses> cl = ddh.getCourses();
		add(new Label("courseCount", ""+cl.getObject().size()));
		add(new GridView<IModel<Courses>>("courseRows",
				new TableDateProvider<Courses>(cl)) {
			private static final long serialVersionUID = 7700775925161603448L;

			@Override
			protected void populateEmptyItem(Item<IModel<Courses>> arg0) {
				arg0.add(new Label("courseName", ""));
			}

			@Override
			protected void populateItem(Item<IModel<Courses>> arg0) {
				arg0.add(new Label("courseName", arg0.getModelObject()
						.getObject().getCourseName()));
			}
		}.setColumns(3));
		
		CayenneList<Users> fl = ddh.getPrimaryAdmins();
		fl.add(ddh.getSecondaryAdmins());
		fl.add(ddh.getTeachers());
		add(new Label("facCount", ""+fl.getObject().size()));
		
		add(new GridView<IModel<Users>>("facultyRows", new TableDateProvider<Users>(fl)){
			private static final long serialVersionUID = 1906434582029512597L;

			@Override
			protected void populateEmptyItem(Item<IModel<Users>> arg0) {
				arg0.add(new Label("facName",""));
			}

			@Override
			protected void populateItem(Item<IModel<Users>> arg0) {
				Users u = arg0.getModelObject().getObject();
				arg0.add(new Label("facName",u.getCompleteUserName()+ " ( "+u.getEmail()+" )"));
			}
		});
		
		CayenneList<Users> sl = ddh.getStudents();
		add(new Label("stCount", ""+sl.getObject().size()));
		
		add(new GridView<IModel<Users>>("studentRows", new TableDateProvider<Users>(sl)){
			private static final long serialVersionUID = 1906434582029512597L;

			@Override
			protected void populateEmptyItem(Item<IModel<Users>> arg0) {
				arg0.add(new Label("studentName",""));
			}

			@Override
			protected void populateItem(Item<IModel<Users>> arg0) {
				Users u = arg0.getModelObject().getObject();
				arg0.add(new Label("studentName",u.getCompleteUserName()+ " ( "+u.getEmail()+" )"));
			}
		}.setColumns(3));
 	}

	private DeptDataHandler ddh;
	private IModel<Departments> deptModel = null;
}
